package WIAB::Model::Forum;

use strict;
use warnings;
use base 'Catalyst::Model';
use open ":utf8";
use Data::UUID;
use HTML::Scrubber;
use File::Slurp;
use File::Basename;
use File::Finder;
use Path::Class;
use Text::Textile;
use YAML qw/LoadFile Dump/;
my $textile = Text::Textile->new();
$textile->css(0);
use DateTime;
my $base_dir = Path::Class::Dir->new(WIAB->config->{forum});

=head1 NAME

WIAB::Model::Forum - Catalyst Model

=head1 DESCRIPTION

Catalyst Model.

=head1 AUTHOR

Kieren Diment C< zarquon@cpan.org >

=head1 LICENSE

This library is free software, you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut



sub untaint_content {
    my ($self, $name, $email, $content) = @_;
    my $scrubber = HTML::Scrubber->new( allow => [ qw/ a p b i u hr br/ ] );
    $name = $scrubber->scrub($name);
    $email = $scrubber->scrub($email);
    $content = $scrubber->scrub($content);
    return ($name, $email, $content);
}

sub get_content {
    my ($self, $files) = @_;
    my @content;
    foreach my $f (@$files) {
        my $c_struct = LoadFile("$f");
        my $c = $textile->process($c_struct->{content});
        push @content, {
            name  => $c_struct->{name},
            email => $c_struct->{email},
            content => $c,
            filename => basename("$f"),
            issued => (stat "$f")[9]},
        };
    $DB::single=1;
    return \@content;
}


sub get_forum_file_list {
    my($self, $root) = @_;
    my @files = map { $_->[1] }
        sort { $a->[0] <=> $b->[0] }
            map  { [ -M $_, $_ ] }
                grep { /\w/ }
                    File::Finder->type('f')->in("$root");

    @files = grep {!/\.svn/ && /\.txl$/} @files;
    map {$_ = Path::Class::File->new($_)} @files;
    return \@files;
}

sub write_content {
    my ($self, $name, $email, $content) = @_;
    ($name, $email, $content) = $self->untaint_content($name, $email, $content);
    my $error = undef;
    my $filename = Data::UUID->new()->create_str() . ".txl";
    my $file_loc = $base_dir->file($filename);
    my $to_write = { name => $name,
                  email =>$email,
                  content => $content};
    $to_write= Dump $to_write;
    
    eval {
        File::Slurp::write_file("$file_loc", $to_write);
    };
    $error = $@ if $@;
    return $error;
}

sub make_rss_feed {
    my ($self, $dir, $base_uri) = @_;
    my @posts = 
    my $feed = XML::Feed->new('RSS');
    $feed->title( "$base_uri/forum " . WIAB->config->{name} . " Forum posts RSS feed");
    $feed->link( $base_uri . "/forum");
    my $files = $self->get_forum_file_list($dir);
    my $all_content = $self->get_content($files);

    foreach my $content ( @$all_content ) {
        my $feed_entry = XML::Feed::Entry->new('RSS');
        $feed_entry->title( "Author: " . $content->{name} );
        $feed_entry->link ($base_uri . '/forum/permalink/'. $content->{filename});
        $feed_entry->summary($textile->process($content->{content}));
        $DB::single=1;
        $feed_entry->issued( DateTime->from_epoch(epoch => $content->{issued} ));
        $feed->add_entry($feed_entry);
    }
    return $feed->as_xml;

    # name email content filename content issued
    
}

1;
